package com.yiqi.zhiyuan.feature.msg.view;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.DensityUtil;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.entity.vo.MessageTypeVO;
import com.yiqi.zhiyuan.entity.vo.MsgVO;
import com.yiqi.zhiyuan.feature.msg.contract.MessageContract;
import com.yiqi.zhiyuan.feature.msg.dependencies.message.DaggerMessageComponent;
import com.yiqi.zhiyuan.feature.msg.dependencies.message.MessagePresenterModule;
import com.yiqi.zhiyuan.feature.msg.view.adapter.EarningsMessageAdapter;
import com.yiqi.zhiyuan.feature.msg.view.adapter.NoticeMessageAdapter;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

public class MsgEarningListActivity extends BaseActivity implements OnRefreshListener, View.OnClickListener,
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, MessageContract.View {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_msg)
    RecyclerView mRvMsg;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    private EmptyView mEmptyView;
    private EarningsMessageAdapter mAdapter;
    private int mPageIndex = 1;
    private int mPageSize = 10;

    private int mMessageType;


    @Inject
    MessageContract.Presenter mPresenter;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_msg_list;
    }

    @Override
    protected void initInjector() {
        DaggerMessageComponent.builder().messagePresenterModule(new MessagePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {

        initAdapter();
    }

    @Override
    protected void initData() {
        mMessageType=this.getIntent().getIntExtra("message_type",-1);
        initToolBar(mToolbar,mMessageType==0?"公告消息":mMessageType==1?"系统消息":mMessageType==2?"行程消息":"公告消息",true,true);
        mPresenter.doMessageList(mMessageType, mPageIndex);
    }

    private void initAdapter() {
        mSmartRefreshLayout.setEnableLoadMore(false);
        mSmartRefreshLayout.setOnRefreshListener(this);
        mEmptyView = new EmptyView(mRvMsg);
        mEmptyView.setErrorButtonClickListener(this);
        mRvMsg.setLayoutManager(new LinearLayoutManager(this));
        mRvMsg.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(DensityUtil.dp2px(10)));
        mAdapter = new EarningsMessageAdapter();
        mAdapter.bindToRecyclerView(mRvMsg);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnLoadMoreListener(this, mRvMsg);
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        refresh();
    }

    @Override
    public void onClick(View v) {
       loadData();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    @Override
    public void showMessageSuccess(MsgVO list) {

    }

    @Override
    public void showMessageListSuccess(List<MessageTypeVO> list) {
        mSmartRefreshLayout.finishRefresh();
        setData(true, list);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showMessageListError(String errorMsg) {
        mSmartRefreshLayout.finishRefresh();
        mAdapter.setNewData(null);
        mAdapter.setEmptyView(mEmptyView.getErrorView(errorMsg));
    }

    @Override
    public void showMessageListEmpty() {
        mSmartRefreshLayout.finishRefresh();
        mAdapter.setNewData(null);
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
    }

    @Override
    public void showMessageListMoreSuccess(List<MessageTypeVO> list) {
        setData(false, list);
    }

    @Override
    public void showMessageListMoreError(String errorMsg) {
        mAdapter.loadMoreFail();
    }

    private void refresh() {
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        loadData();
    }

    private void loadMore() {
        mPresenter.doMessageMoreList(mMessageType, mPageIndex);
    }

    private void setData(boolean isRefresh, List data) {
        mPageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < mPageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    private void loadData() {
        mPageIndex = 1;
        mPresenter.doMessageList(mMessageType, mPageIndex);
    }
}
